package pattern.proxy.dynamic_proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class ProxyPoint implements MethodInterceptor {
    public TrainStation getProxyObject(Class<? extends TrainStation> trainStation) {
        // 创建Enhancer对象，类似于jdk动态代理的Proxy类
        Enhancer enhancer = new Enhancer();
        // 设置父类的字节码
        enhancer.setSuperclass(trainStation);
        // 设置回调函数
        enhancer.setCallback(this);
        // 创建代理对象并返回
        return (TrainStation) enhancer.create();
    }
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("代售点加收5%手续费1");
        return methodProxy.invokeSuper(o, objects);
    }
}
